<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
</head>

<body>

    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div class="navbar-item">
                        <a class="button is-small is-primary" style="background-color: #315790;" href="register.html">注册</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>


    <div class="container">
        <div class="box sign-box">
            <div>
                <div class="field">
                    <label class="label">用户名</label>
                    <div class="control">
                        <input class="input" id="username" type="text" autocomplete="off" placeholder="请输入用户名">
                    </div>
                </div>
                <div class="field">
                    <label class="label">密码</label>
                    <div class="control">
                        <!-- 禁止自动填充用户名密码 -->
                        <input type="password" style="position: absolute;left: 0;display: none" />
                        <input class="input" id="password" type="password" placeholder="请输入密码">
                    </div>
                </div>
                <div class="field is-grouped">
                    <div class="control">
                        <button class="button is-primary" onclick="doLogin()">立即登录</button>
                    </div>
                    <div class="control">
                        <a href="register.html" class="button is-text" style="text-decoration: none;">还没账号？</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>

        // 登录功能
        function doLogin() {
            // 1. 非空校验
            var username = jQuery("#username");
            var password = jQuery("#password");

            if (username.val().trim() == "") {
                alert("请先输入用户名!");
                username.focus();
                return false;
            }
            if (password.val().trim() == "") {
                alert("请先输入密码!");
                passworde.focus();
                return false;
            }

            // 2. 发送 ajax 请求
            jQuery.ajax({
                url: req_base_url + "user/login",
                method: "POST",
                data: {
                    "username": username.val(),
                    "password": password.val()
                },
                success: function (res) {
                    // 3. 将执行的结果展示到前端 {"code": 200, "msg": "状态码描述信息", "date": []}
                    if (res != null && res.code == 200) {
                        // 登录成功
                        // 将服务器端生成的 Token 保存起来
                        window.localStorage.setItem(token_key, res.data.token);
                        window.localStorage.setItem("uid", res.data.uid);
                        window.localStorage.setItem("manager", res.data.manager);
                        window.localStorage.setItem("username", res.data.username);
                        // alert("恭喜: 登录成功!");
                        location.href = "index.html";
                    } else {
                        // 登录失败
                        location.href="error.html";
                        alert("抱歉: 登录失败! " + res.msg)
                    }

                }
            })


        }
    </script>
</body>

</html>